{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "5678b63a-3352-4256-8691-d9578a292fa9",
   "metadata": {},
   "source": [
    "## Decomposition for a representative type-B mothers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "56a0e031-ed12-4bb1-9820-7183d7a4c0e4",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import math\n",
    "from class_decomposition import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "91552552-8828-48c0-88a2-3c69a7fd5875",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# calibrated parameters\n",
    "pi_s = 1\n",
    "pi_q = 0\n",
    "tau = 0.075\n",
    "\n",
    "gamma = 0.424\n",
    "\n",
    "# estimation results:\n",
    "pi_n = 1.466967\n",
    "pi_nq = 0.110715\n",
    "\n",
    "theta = 0.927835\n",
    "alpha = 0.612176\n",
    "rho = -7.919992\n",
    "\n",
    "eps_cutoff = -0.140181\n",
    "\n",
    "# means\n",
    "theta_B = theta - 0.2457272\n",
    "\n",
    "y_B = 24.97204\n",
    "\n",
    "pi_n_tau_B = pi_n + tau * y_B\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "307a2a30-d14f-4306-99da-0ba44a7a176b",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# create an instance of FamilyRation() for the average type-B mother\n",
    "FR_B = FamilyRation(\n",
    "    pi_n = pi_n_tau_B, pi_nq = pi_nq, pi_s = pi_s, pi_q = pi_q, \n",
    "    theta = theta_B, rho = rho, gamma = gamma, \n",
    "    alpha = alpha, y = y_B\n",
    "    )  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "3fe169b6-6034-42cc-8b29-bae089d4dfe4",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# decomposition, average type-B mother\n",
    "D = Decomposition(FR_B)\n",
    "\n",
    "type_B_list = []\n",
    "extract_columns = ['q', 'total', 'price', 'substitution', 'income']\n",
    "    \n",
    "nseq = np.arange(start = 1, stop = 5.1, step = 0.1)\n",
    "\n",
    "for i in nseq:\n",
    "    # run simulation\n",
    "    D.nxset(n = [i], x0 = [0.1, 0.1])\n",
    "    D.run()\n",
    "    D.getDataDecomp_e()\n",
    "    \n",
    "    # define the list of column names to extract from D.dData_e\n",
    "    type_B_list.append(D.dData_e[extract_columns])\n",
    "    \n",
    "df_B = pd.concat(type_B_list).reset_index(drop=True)\n",
    "df_B = df_B.assign(n = nseq)\n",
    "df_B.to_csv('./decompose_averageB.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fa294480-4a36-4af4-b888-bfb0a7bb409a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:base] *",
   "language": "python",
   "name": "conda-base-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
